<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\car;


use app\adminapi\controller\AuthController;
use app\services\car\CarOrderServices;
use think\facade\App;

class CarOrder extends AuthController
{
    public function __construct(App $app,CarOrderServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 获取tab数据
     * @return mixed
     */
    public function getTabsData(){
        $where = $this->request->getMore([
            ['keyword',''],
        ]);
        $admin = $this->request->adminInfo();
        if($admin['store_id']) $where['place_id'] = $admin['store_id'];
        return app('json')->success($this->services->getTabsData($where));
    }

    /**
     * 列表
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderList(){
        $where = $this->request->getMore([
            ['status',''],
            ['keyword',''],
            ['field_key',''],
            ['pay_type',''],
        ]);
        $admin = $this->request->adminInfo();
        if($admin['store_id']) $where['place_id'] = $admin['store_id'];
        return app('json')->success($this->services->getList($where,true,'admin_side'));
    }

    /**
     * 订单日志
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderLog(){
        $where = $this->request->getMore([
            ['order_id',''],
        ]);
        if(!$where['order_id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->getOrderLog((int)$where['order_id']));
    }

    /**
     * 订单流水
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderFlow(){
        $where = $this->request->getMore([
            ['order_id',''],
        ]);
        if(!$where['order_id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->getOrderFlow((int)$where['order_id']));
    }


    /**
     * 订单详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderInfo(){
        $where = $this->request->getMore([
            ['order_id',''],
        ]);
        if(!$where['order_id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->getOrderInfo((int)$where['order_id']));
    }

    /**
     * 完成订单
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderClosing(){
        $data = $this->request->postMore([
            ['order_id',0],
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        $admin = $this->request->adminInfo();
        $admin['user_name'] = $admin['real_name'];
        return app('json')->success($this->services->orderClosing((int)$data['order_id'],$admin,3));
    }

    /**
     * 押金详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function depositInfo(){
        $data = $this->request->getMore([
            ['order_id',0]
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->depositInfo((int)$data['order_id']));
    }

    /**
     * 押金退款
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function depositRefund(){
        $data = $this->request->postMore([
            ['order_id',''],
            ['price',''],
            ['deposit_type',''],
            ['content',''],
            ['illegal_remark',''],
            ['return_type',''],
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        if(!$data['price']) return app('json')->fail('请输入退款金额');
        if(!$data['content']) return app('json')->fail('请输入退款说明');
        if($data['deposit_type']) if(!$data['illegal_remark']) return app('json')->fail('请输入违章说明');
        $op_user = $this->request->adminInfo();
        return app('json')->success($this->services-> depositRefund((int)$data['order_id'],$data,$op_user,3));
    }
}
